$("#findUser").on("click", function () {
    $.get("agentrobot_livestreamer_list/findUser", {user: $("#c-livestreamerUser").val()}, function (data) {
        if (data != null) {
            $("#c-nickname").attr("value", data.nickname)
        }
    })
})

$(function () {
    $("#c-livestreamerUser").val($("#nowUser").val());

    $("#findUser").click()
})

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'agentrobot_livestreamer_list/index' + location.search,
                    // add_url: 'vip_user/add',
                    // edit_url: 'vip_user/edit',
                    // del_url: 'vip_user/del',
                    // multi_url: 'vip_user/multi',
                    // import_url: 'vip_user/import',
                    table: 'agentrobot_livestreamer_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'livestreamerUser',
                sortName: 'followTime',
                templateView: false,
                clickToSelect: false,
                search: false,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                searchFormVisible: true,
                columns: [
                    [
                        {
                            field: 'agentID',
                            title: __('Agent'),
                            searchList: Config.adminUserList,
                            defaultValue: $("#loginUserId").val(),
                            formatter: Table.api.formatter.label,
                            visible: false
                        },
                        {
                            field: 'robotUser',
                            title: __('Agent Robot'),
                            searchList: Config.robotUser,
                            formatter: Table.api.formatter.label
                        },
                        {field: 'livestreamerUser', title: __('Anchor ID'), operate: false},
                        {
                            field: 'gender', title: __('Sex'), operate: false, formatter: function (value) {
                                if (value == 0)
                                    return "未知";
                                else if (value == 1)
                                    return "男";
                                else if (value == 2)
                                    return "女";
                                else if (value == 3)
                                    return "中性";
                            },
                        },
                        {field: 'fansNum', title: __('Followers'), operate: false},
                        {
                            field: 'checkStatus',
                            title: __('Contractable status'),
                            searchList: Config.checkStatus,
                            formatter: function (value) {
                                if (value == '-1')
                                    return "未知"
                                else if (value == 1)
                                    return "可签约"
                                else if (value == '0')
                                    return '不可签';
                            }
                        },
                        {
                            field: 'grade',
                            title: __('Anchor level'),
                            searchList: Config.grade,
                            formatter: function (value) {
                                if (value == '-1')
                                    return "未知"
                                else if (value == 1)
                                    return "及格"
                                else if (value == 2)
                                    return "优质"
                                else if (value == '0')
                                    return '放弃';
                            }
                        },
                        {
                            field: 'isFollow', title: __('Is Following'), operate: false,
                            formatter: function (value) {
                                if (value == 0 )
                                    return "否";
                                else if (value == 1)
                                    return "是";
                            }
                        },
                        {field: 'followTime', title: __('Follwing time'), operate: false},
                        {
                            field: 'followTime',
                            title: 'Follwing time',
                            searchList: {"1": "7天以内", "2": "14天以内", "3": "30天以内", "4": "3个月以内"},
                            visible: false
                        },
                        {field: 'followRobotNum', title: __('The number of accounts followed'), operate: false},
                        {
                            field: 'isRefollow',
                            title: __('follow back or not'),
                            searchList: {"0": "未回关", "1": "已回关"},
                            formatter: function (value) {
                                if (value == 0)
                                    return "否";
                                else if (value == 1)
                                    return "是";
                            }
                        },
                        {field: 'refollowTime', title: __('Follow back date'), operate: false},
                        {field: 'refollowRobotNum', title: __('The number of accounts that are followed back'), operate: false},
                        {
                            field: 'signStatus',
                            title: __('Whether to sign a contract or not'),
                            searchList: {"0": "未签约", "1": "已签约"},
                            formatter: function (value) {
                                if (value == 0)
                                    return "否";
                                else if (value == 1)
                                    return "是";
                            }
                        },
                        {
                            field: 'contactType',
                            title: __('Contact type'),
                            searchList: {"0": "无", "1": "instagram", "2": "facebook", "3": "whatsapp", "4": "wechat"},
                            formatter: function (value) {
                                if (value == 0)
                                    return "无";
                                else if (value == 1)
                                    return "instagram";
                                else if (value == 2)
                                    return "facebook";
                                else if (value == 3)
                                    return "whatsapp";
                                else if (value == 4)
                                    return "wechat";
                            }
                        },
                        {field: 'contact', title: __('Contact Information'), operate: false},
                        {field: 'twitter_id', title: __('twitter'), operate: false},
                        {field: 'youtube_channel_id', title: __('youtube'), operate: false},
                        {field: 'memo', title: __('Track progress'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'follow',
                                    text: '填写跟踪记录',
                                    title: '填写跟踪记录',
                                    classname: 'btn btn-xs btn-success btn-dialog btn-follow follow',
                                    refresh: true,
                                    url: "agentrobot_livestreamer_list/follow"
                                },
                                {
                                    name: 'sign',
                                    text: '签约登记',
                                    title: '签约登记',
                                    classname: 'btn btn-xs btn-success btn-dialog btn-sign sign',
                                    refresh: true,
                                    url: "agentrobot_livestreamer_list/sign"
                                },
                                {
                                    name: 'recheck',
                                    text: '重新验证签约状态',
                                    title: '重新验证签约状态',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-ajax btn-stop',
                                    refresh: true,
                                    confirm: function () {
                                        return '是否确定重新验证签约状态？';
                                    },
                                    url: "agentrobot_livestreamer_list/recheck"
                                },
                                {
                                    name: 'discard',
                                    text: '放弃该用户',
                                    title: '放弃该用户',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-ajax btn-stop',
                                    refresh: true,
                                    url: "agentrobot_livestreamer_list/discard",
                                    confirm: function () {
                                        return '是否确定放弃该用户？';
                                    },
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        follow: function () {
            Controller.api.bindevent()
        },
        sign: function () {
            Controller.api.bindevent()
        }
    };
    return Controller;
});
